package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

/**
 * @author 里
 * @Data:2022/01/01/0:05
 * @Description:
 */
@Configuration
public class CorsConfig {

    @Bean
    public WebFilter webFilter (){

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 设置允许所有跨域
        corsConfiguration.addAllowedMethod("*"); // 设置允许跨域的方法
        corsConfiguration.addAllowedHeader("*"); // 允许请求头设置数据
        corsConfiguration.setAllowCredentials(true); // 允许携带cookie数据
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(corsConfigurationSource);
    }
}
